package data

import (
	"github.com/go-kratos/kratos/v2/log"
	"github.com/go-redis/redis"
	"github.com/google/wire"

	"VideoMemberCard/app/payment/service/internal/conf"
)

// ProviderSet is data providers.
var ProviderSet = wire.NewSet(NewData, NewPaymentRepo, NewRedis)

// Data .
type Data struct {
	db  *redis.Client
	log *log.Helper
}

func NewRedis(conf *conf.Data_Redis) *redis.Client {
	var options redis.Options
	options.Addr = conf.Addr
	client := redis.NewClient(&options)
	return client
}

// NewData .
func NewData(database *redis.Client, logger log.Logger) (*Data, func(), error) {
	log := log.NewHelper(log.With(logger, "module", "payment-service/data"))

	d := &Data{
		db:  database,
		log: log,
	}
	return d, func() {
		if err := d.db.Close(); err != nil {
			log.Error(err)
		}
	}, nil
}
